package cn.dm.service;

import cn.dm.mapper.DmImageMapper;
import cn.dm.pojo.DmImage;
import cn.dm.common.Constants;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import cn.dm.common.EmptyUtils;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Service
@Slf4j
public class DmImageServiceImpl implements DmImageService {
    @Resource
    private DmImageMapper dmImageMapper;
    @Override
    public DmImage getDmImageById(Long id) throws Exception {
        return dmImageMapper.getDmImageById(id);
    }

    @Override
    public List<DmImage> getDmImageListByMap(Map<String, Object> param) throws Exception {
        return dmImageMapper.getDmImageListByMap(param);
    }

    @Override
    public Integer getDmImageCountByMap(Map<String, Object> param) throws Exception {
        return dmImageMapper.getDmImageCountByMap(param);
    }

    @Override
    public Integer qdtxAddDmImage(DmImage dmImage) throws Exception {
        return dmImageMapper.insertDmImage(dmImage);
    }

    @Override
    public Integer qdtxModifyDmImage(DmImage dmImage) throws Exception {
        return dmImageMapper.updateDmImage(dmImage);
    }

    @Override
    public List<DmImage> queryDmImageList(Long targetId, Integer type, Integer category) throws Exception {
        Map<String, Object> param=new HashMap<String, Object>();
        param.put("targetId",targetId);
        param.put("type",type);
        param.put("category",category);
        List<DmImage> dmImageList=dmImageMapper.getDmImageListByMap(param);
        log.info("=================dmImageList:"+dmImageList.size());
        //如何有图片，则设置图片，没有图片，会设置默认的图片路径
        if(dmImageList.size()==0){
            dmImageList=new ArrayList<DmImage>();
            DmImage dmImage=new DmImage();
            dmImage.setCategory(category);
            dmImage.setType(type);
            dmImage.setTargetId(targetId.intValue());
            dmImageList.add(dmImage);
            for (DmImage image:dmImageList){
                String defaultImage=null;
                if(EmptyUtils.isEmpty(dmImage.getCategory())){
                    defaultImage=Constants.DEFAULT_NORMAL;
                }else if(dmImage.getCategory()==Constants.Image.ImageCategory.user){
                    defaultImage=Constants.DEFAULT_USER;
                }else if(dmImage.getCategory()==Constants.Image.ImageCategory.item){
                    if(dmImage.getType()==Constants.Image.ImageType.normal){
                        defaultImage=Constants.DEFAULT_NORMAL;
                    }else if(dmImage.getType()==Constants.Image.ImageType.carousel){
                        defaultImage=Constants.DEFAULT_CAROUSEL;
                    }else if(dmImage.getType()==Constants.Image.ImageType.poster){
                        defaultImage=Constants.DEFAULT_POSTER;
                    }
                }
                image.setImgUrl(EmptyUtils.isNotEmpty(image.getImgUrl())?image.getImgUrl():defaultImage);
            }
        }
        return dmImageList;
    }
}
